"""
项目配置文件
包含所有系统组件的配置参数
"""
import os
from typing import List, Dict, Any
from pydantic import BaseSettings


class Settings(BaseSettings):
    """系统配置类"""
    
    # 基础配置
    PROJECT_NAME: str = "SecVulnKG"
    VERSION: str = "1.0.0"
    DEBUG: bool = False
    
    # 服务配置
    API_HOST: str = "0.0.0.0"
    API_PORT: int = 8000
    WORKERS: int = 4
    
    # 数据库配置
    # MongoDB
    MONGODB_URL: str = "mongodb://localhost:27017"
    MONGODB_DB: str = "security_vulns"
    
    # Neo4j图数据库
    NEO4J_URI: str = "bolt://localhost:7687"
    NEO4J_USER: str = "neo4j"
    NEO4J_PASSWORD: str = "password"
    
    # Redis缓存
    REDIS_URL: str = "redis://localhost:6379/0"
    REDIS_CACHE_TTL: int = 3600
    
    # Elasticsearch
    ELASTICSEARCH_HOST: str = "localhost"
    ELASTICSEARCH_PORT: int = 9200
    ELASTICSEARCH_INDEX: str = "security_vulns"
    
    # 消息队列配置
    # Celery
    CELERY_BROKER_URL: str = "redis://localhost:6379/1"
    CELERY_RESULT_BACKEND: str = "redis://localhost:6379/2"
    
    # Kafka
    KAFKA_BOOTSTRAP_SERVERS: List[str] = ["localhost:9092"]
    KAFKA_TOPIC_RAW_DATA: str = "raw_data"
    KAFKA_TOPIC_EXTRACTED_ENTITIES: str = "extracted_entities"
    
    # 爬虫配置
    SPIDER_CONCURRENT_REQUESTS: int = 32
    SPIDER_DOWNLOAD_DELAY: float = 1.0
    SPIDER_USER_AGENTS: List[str] = [
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
    ]
    
    # 代理配置
    PROXY_POOL_SIZE: int = 100
    PROXY_TEST_URL: str = "http://httpbin.org/ip"
    
    # 数据源配置
    DATA_SOURCES: Dict[str, Dict[str, Any]] = {
        "cve_mitre": {
            "url": "https://cve.mitre.org/",
            "enabled": True,
            "priority": 1
        },
        "nvd_nist": {
            "url": "https://nvd.nist.gov/",
            "enabled": True,
            "priority": 1
        },
        "exploit_db": {
            "url": "https://www.exploit-db.com/",
            "enabled": True,
            "priority": 2
        },
        "github_advisories": {
            "url": "https://github.com/advisories",
            "enabled": True,
            "priority": 2
        },
        "security_blogs": {
            "urls": [
                "https://krebsonsecurity.com/",
                "https://www.schneier.com/",
                "https://threatpost.com/"
            ],
            "enabled": True,
            "priority": 3
        }
    }
    
    # 大语言模型配置
    # OpenAI
    OPENAI_API_KEY: str = ""
    OPENAI_MODEL: str = "gpt-4-turbo-preview"
    OPENAI_MAX_TOKENS: int = 4000
    OPENAI_TEMPERATURE: float = 0.1
    
    # 本地模型配置
    LOCAL_MODEL_PATH: str = "models/"
    LOCAL_MODEL_NAME: str = "chinese-roberta-wwm-ext"
    GPU_ENABLED: bool = True
    BATCH_SIZE: int = 32
    
    # 实体抽取配置
    ENTITY_TYPES: List[str] = [
        "vulnerability_name",      # 漏洞名称
        "cve_id",                 # CVE编号
        "product_name",           # 产品名称
        "vendor_name",            # 厂商名称
        "severity_score",         # 严重程度评分
        "attack_vector",          # 攻击向量
        "vulnerability_type",     # 漏洞类型
        "affected_version",       # 影响版本
        "patch_info",             # 补丁信息
        "exploit_code"            # 利用代码
    ]
    
    RELATION_TYPES: List[str] = [
        "affects",                # 影响关系
        "exploits",              # 利用关系
        "patches",               # 补丁关系
        "similar_to",            # 相似关系
        "prerequisite_of",       # 前置条件
        "leads_to"               # 引发关系
    ]
    
    # 知识图谱配置
    KG_BATCH_SIZE: int = 1000
    KG_MAX_RELATIONS_PER_ENTITY: int = 50
    KG_SIMILARITY_THRESHOLD: float = 0.8
    
    # 文件存储配置
    DATA_DIR: str = "data/"
    RAW_DATA_DIR: str = "data/raw/"
    PROCESSED_DATA_DIR: str = "data/processed/"
    MODEL_DIR: str = "models/"
    LOG_DIR: str = "logs/"
    
    # 日志配置
    LOG_LEVEL: str = "INFO"
    LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    LOG_MAX_SIZE: int = 10 * 1024 * 1024  # 10MB
    LOG_BACKUP_COUNT: int = 5
    
    # 监控配置
    PROMETHEUS_PORT: int = 8080
    HEALTH_CHECK_INTERVAL: int = 30
    
    # 安全配置
    SECRET_KEY: str = "your-secret-key-here"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
    
    # 性能配置
    MAX_WORKERS: int = 8
    CHUNK_SIZE: int = 1000
    MEMORY_LIMIT: str = "8GB"
    
    class Config:
        env_file = ".env"
        case_sensitive = True


# 创建全局配置实例
settings = Settings() 